home *** CD-ROM | disk | FTP | other *** search
/ Cracking 1 / Cracking I..iso / Tools / Ostatní / aPLib v0.26b / src / c / apdepack.c next >
Encoding:
C/C++ Source or Header  |  2001-12-15  |  3.1 KB  |  145 lines

  1. /*
  2.  * aPLib compression library  -  the smaller the better :)
  3.  *
  4.  * C depacker
  5.  *
  6.  * Copyright (c) 1998-2000 by Joergen Ibsen / Jibz
  7.  * All Rights Reserved
  8.  */
  9.  
  10. #include "apdepack.h"
  11.  
  12. /* global variables used */
  13. unsigned char *aP_d_output;
  14. unsigned char *aP_d_input;
  15. unsigned char *aP_d_tagbyte;
  16. unsigned int aP_d_tagpos;
  17.  
  18. /* input functions */
  19. unsigned int aP_d_getbit()
  20. {
  21.    unsigned int tmp;
  22.  
  23.    if (!aP_d_tagpos--)
  24.    {
  25.       aP_d_tagpos = 7;
  26.       aP_d_tagbyte = aP_d_input;
  27.       aP_d_input++;
  28.    }
  29.  
  30.    tmp = ((*aP_d_tagbyte) >> 7) & 0x01;
  31.    *aP_d_tagbyte = *aP_d_tagbyte << 1;
  32.  
  33.    return (tmp);
  34. }
  35.  
  36. unsigned int aP_d_getgamma()
  37. {
  38.    unsigned int result = 1;
  39.  
  40.    do {
  41.       result = (result << 1) + aP_d_getbit();
  42.    } while (aP_d_getbit());
  43.  
  44.    return (result);
  45. }
  46.  
  47. /* the main depacking function */
  48. unsigned int aP_depack(unsigned char *source,
  49.                        unsigned char *destination)
  50. {
  51.    unsigned int offs, len, R0;
  52.    int done;
  53.    int i;
  54.  
  55.    aP_d_input = source;
  56.    aP_d_output = destination;
  57.  
  58.    aP_d_tagpos = 0;
  59.  
  60.    *aP_d_output = *aP_d_input;
  61.    aP_d_output++;
  62.    aP_d_input++;
  63.  
  64.    done = 0;
  65.  
  66.    while (!done)
  67.    {
  68.       if (aP_d_getbit())
  69.       {
  70.          if (aP_d_getbit())
  71.          {
  72.             if (aP_d_getbit())
  73.             {
  74.                offs = 0;
  75.                for (i = 4; i; i--) offs = (offs << 1) + aP_d_getbit();
  76.  
  77.                if (offs)
  78.                {
  79.                   *aP_d_output = *(aP_d_output - offs);
  80.                   aP_d_output++;
  81.                } else {
  82.                   *aP_d_output = 0x00;
  83.                   aP_d_output++;
  84.                }
  85.             } else {
  86.                offs = *aP_d_input;
  87.                aP_d_input++;
  88.  
  89.                len = 2 + (offs & 0x0001);
  90.                offs >>= 1;
  91.  
  92.                if (offs)
  93.                {
  94.                   for (; len; len--)
  95.                   {
  96.                      *aP_d_output = *(aP_d_output - offs);
  97.                      aP_d_output++;
  98.                   }
  99.                } else done = 1;
  100.                R0 = offs;
  101.             }
  102.          } else {
  103.             offs = aP_d_getgamma();
  104.  
  105.             if (offs == 2)
  106.             {
  107.                offs = R0;
  108.  
  109.                len = aP_d_getgamma();
  110.  
  111.                for (; len; len--)
  112.                {
  113.                   *aP_d_output = *(aP_d_output - offs);
  114.                   aP_d_output++;
  115.                }
  116.             } else {
  117.                offs -= 3;
  118.                offs <<= 8;
  119.                offs += *aP_d_input;
  120.                aP_d_input++;
  121.  
  122.                len = aP_d_getgamma();
  123.  
  124.                if (offs >= 32000) len++;
  125.                if (offs >= 1280) len++;
  126.                if (offs < 128) len += 2;
  127.  
  128.                for (; len; len--)
  129.                {
  130.                   *aP_d_output = *(aP_d_output - offs);
  131.                   aP_d_output++;
  132.                }
  133.                R0 = offs;
  134.             }
  135.          }
  136.       } else {
  137.          *aP_d_output = *aP_d_input;
  138.          aP_d_output++;
  139.          aP_d_input++;
  140.       }
  141.    }
  142.  
  143.    return (aP_d_output - destination);
  144. }
  145.